package com.envper.controller.device;

import com.envper.common.domain.dto.DeviceLogDto;
import com.envper.common.service.device.DeviceLogService;
import com.envper.mysql.domain.Result;
import com.envper.mysql.domain.Status;
import com.envper.mysql.domain.entity.TDeviceLog;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/device/log")
public class LogController {
    private final DeviceLogService deviceLogService;

    @GetMapping("/list")
    public Result<List<DeviceLogDto>> getDeviceLogList(
            @RequestParam(name = "deviceId", required = false) String deviceId,
            @RequestParam(name = "limit", required = false) Long limit
    ) {
        List<DeviceLogDto> deviceLogDtoList = new ArrayList<>();
        if (StringUtils.hasText(deviceId)) {
            deviceLogDtoList = deviceLogService.getDeviceLogListByDeviceId(deviceId, limit);
        } else {
            deviceLogDtoList = deviceLogService.getDeviceLogList();
        }
        return Result.ok(Status.SUCCESS, deviceLogDtoList);
    }


}
